package cn.snmp.mutual.nms;

import cn.snmp.mutual.uk.stack.*;

public class ObjectConversion
{
    public static String[] oid2string(final SnmpOID[] objectList) {
        if (objectList == null) {
            return null;
        }
        final String[] stringList = new String[objectList.length];
        for (int i = 0; i < objectList.length; ++i) {
            stringList[i] = objectList[i].toString();
        }
        return stringList;
    }
    
    public static SnmpOID[] string2oid(final String[] stringList) {
        if (stringList == null) {
            return null;
        }
        final SnmpOID[] objectList = new SnmpOID[stringList.length];
        for (int i = 0; i < stringList.length; ++i) {
            objectList[i] = new SnmpOID(stringList[i]);
        }
        return objectList;
    }
    
    public static SnmpVar changeVariable(final AsnObject src) {
        try {
            final byte vartype = src.getRespType();
            if (AsnObject.debug > 6) {
                System.out.println("type is [" + src.getRespTypeString() + "], class name is [" + src.getClass().toString() + "].");
            }
            SnmpVar desc = null;
            final String value = src.toString();
            if (vartype == 2) {
                desc = SnmpVar.createVariable(value, (byte)2);
            }
            else if (vartype == 4) {
                final byte[] strByteList = ((AsnOctets)src).getBytes();
                desc = new SnmpString(strByteList);
            }
            else if (vartype == 6) {
                if (AsnObject.debug > 6) {
                    System.out.println("src ------> " + value);
                }
                if (value.startsWith(".")) {
                    desc = SnmpVar.createVariable(value, (byte)6);
                }
                else {
                    desc = SnmpVar.createVariable("." + value, (byte)6);
                }
                if (AsnObject.debug > 6) {
                    System.out.println("des ------> " + desc.toString());
                }
            }
            else if (vartype == 5) {
                desc = SnmpVar.createVariable(value, (byte)5);
            }
            else if (vartype == 64) {
                desc = SnmpVar.createVariable(value, (byte)64);
            }
            else if (vartype == 3) {
                desc = SnmpVar.createVariable(value, (byte)3);
            }
            else if (vartype == 65) {
                desc = SnmpVar.createVariable(value, (byte)65);
            }
            else if (vartype == 66) {
                desc = SnmpVar.createVariable(value, (byte)66);
            }
            else if (vartype == 67) {
                desc = SnmpVar.createVariable(value, (byte)67);
            }
            else if (vartype == 68) {
                desc = SnmpVar.createVariable(value, (byte)68);
            }
            else if (vartype == 69) {
                desc = SnmpVar.createVariable(value, (byte)69);
            }
            else if (vartype == 70) {
                desc = SnmpVar.createVariable(value, (byte)70);
            }
            else if (vartype == 71) {
                desc = SnmpVar.createVariable(value, (byte)71);
            }
            else {
                desc = SnmpVar.createVariable(value, (byte)5);
                if (AsnObject.debug > 6) {
                    System.out.println("Type String Name : [" + src.getRespTypeString() + "] not found...");
                }
            }
            return desc;
        }
        catch (Exception e) {
            if (AsnObject.debug > 6) {
                e.printStackTrace();
            }
            return null;
        }
    }
    
    public static AsnObject[] changeVariable(final SnmpVar[] src) {
        if (src == null) {
            return null;
        }
        final AsnObject[] des = new AsnObject[src.length];
        for (int i = 0; i < src.length; ++i) {
            des[i] = changeVariable(src[i]);
        }
        return des;
    }
    
    public static SnmpVar[] changeVariable(final AsnObject[] src) {
        if (src == null) {
            return null;
        }
        final SnmpVar[] des = new SnmpVar[src.length];
        for (int i = 0; i < src.length; ++i) {
            des[i] = changeVariable(src[i]);
        }
        return des;
    }
    
    public static AsnObject changeVariable(final SnmpVar src) {
        try {
            final byte vartype = src.getType();
            AsnObject desc = null;
            final String value = src.toString();
            if (vartype == 2) {
                desc = new AsnInteger(((SnmpInt)src).intValue());
            }
            else if (vartype == 4) {
                final byte[] strByteList = ((SnmpString)src).toBytes();
                desc = new AsnOctets(strByteList);
            }
            else if (vartype == 6) {
                if (AsnObject.debug > 6) {
                    System.out.println("src ------> " + value);
                }
                desc = new AsnObjectId(value);
                if (AsnObject.debug > 6) {
                    System.out.println("des ------> " + desc.toString());
                }
            }
            else if (vartype == 5) {
                desc = new AsnNull();
            }
            else if (vartype == 64) {
                desc = new AsnOctets(((SnmpIpAddress)src).toBytes(), (byte)64);
            }
            else if (vartype == 3) {
                desc = new AsnOctets(((SnmpBitstring)src).toBytes(), (byte)3);
            }
            else if (vartype == 65) {
                desc = new AsnUnsInteger(((SnmpCounter)src).longValue(), (byte)65);
            }
            else if (vartype == 66) {
                desc = new AsnUnsInteger(((SnmpGauge)src).longValue(), (byte)66);
            }
            else if (vartype == 67) {
                desc = new AsnUnsInteger(((SnmpTimeticks)src).longValue(), (byte)67);
            }
            else if (vartype == 68) {
                desc = new AsnOctets(((SnmpOpaque)src).toBytes(), (byte)68);
            }
            else if (vartype == 69) {
                desc = new AsnOctets(((SnmpNsap)src).toBytes(), (byte)69);
            }
            else if (vartype == 70) {
                desc = new AsnUnsInteger64(((SnmpCounter64)src).toBigInteger().longValue());
            }
            else if (vartype == 71) {
                desc = new AsnUnsInteger(((SnmpUnsignedInt)src).longValue(), (byte)71);
            }
            else if (AsnObject.debug > 6) {
                System.out.println("Type String Name : [" + src.getTypeString() + "] not found...");
            }
            return desc;
        }
        catch (Exception e) {
            if (AsnObject.debug > 6) {
                e.printStackTrace();
            }
            return null;
        }
    }
}
